package com.cqjtu.store.cart.controller;

import com.cqjtu.store.address.pojo.entity.Address;
import com.cqjtu.store.cart.pojo.dto.*;
import com.cqjtu.store.cart.pojo.vo.CartVO;
import com.cqjtu.store.cart.service.CartService;
import com.cqjtu.store.common.JwtUtils;
import com.cqjtu.store.common.R;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("api/cart")
@Api(tags = "购物车接口")
@Slf4j
public class CartController {
    @Autowired
    private CartService cartService;
    /**
     * 查询购物车列表
     * @return
     */
    @PostMapping("list")
    @ApiOperation("购物车列表查询接口")
    public R<List<CartVO>> geCartList(@RequestBody Integer[] selectedIds,HttpServletRequest request){
        System.out.println("selectedIds:---------------------"+ Arrays.toString(selectedIds));

        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        //查询订单列表
        List<CartVO> cartList = cartService.getCartList(selectedIds,userLoginVO);
        return R.ok(cartList);
    }
    @PutMapping("/{id}/{flag}")
    @ApiOperation("更新购物车商品数量接口")
    public R updateCartNumById(@PathVariable("id") Integer id,
                               @PathVariable("flag") Integer flag,
                               HttpServletRequest request
    ){
        String token = request.getHeader("Authorization");
        UserLoginVO loinUser = JwtUtils.parseToken(token);
        return  cartService.updateCartNumById(id,flag,loinUser.getId());
    }

    @PostMapping("/insert")
    @ApiOperation("添加购物车接口")
    public R<Void> insert(@RequestBody @Validated  CartInsertDTO cartInsertDTO, HttpServletRequest request){
        System.out.println("cartInsertDTO:"+cartInsertDTO);
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        // System.out.println("userLoginVO:"+userLoginVO);
        cartService.insert(cartInsertDTO,userLoginVO);
        return R.ok();

    }
    /**
     * 删除购物车单条信息
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R<Void> delete (@PathVariable("id") Integer id,HttpServletRequest request){

        //获取用户身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO=JwtUtils.parseToken(token);
        cartService.delete(id,userLoginVO);
        return R.ok();
    }
    /**
     * 删除购物车多条信息
     * @return
     */
    @DeleteMapping("deleteMore")
    public R<Void> deleteMore(@RequestBody CartDeletesDTO[] cartDDTOs , HttpServletRequest request){
        /*log.debug("删除购物车物品的种类:{}",cartDDTOs.length);
        for (int i=0;i<cartDDTOs.length;i++){
            log.debug("删除的id:{}",cartDDTOs[i].getIds());
        }*/
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO=JwtUtils.parseToken(token);
        cartService.deleteMore(cartDDTOs,userLoginVO);
        return R.ok();
    }

    @DeleteMapping()
    public R<Void> removeByIds(@RequestBody Integer[] ids, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        cartService.removeByIds(ids, userLoginVO);
        return R.ok();
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("clear")
    public R<Void> clear(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        cartService.clear(userLoginVO);
        return R.ok();
    }

    @PostMapping("count")
    @ApiOperation("结算购物车接口")
    public R<Void> countCartList(@RequestBody @Validated CountDTO countDTO, HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        //查询订单列表
        cartService.countCartList(countDTO,userLoginVO);
        return R.ok();
    }

}
